package gitobject;

import java.io.*;

public class Branch extends GitObject implements Serializable {


    public static String branchPath = repository.Repository.getGitDir() + File.separator + "refs" + File.separator
            + "heads";
    public String name;

    public Branch(String hash, String name) throws Exception {
        this.key = hash;
        this.name = name;
        Head.setName(name);
        this.writeObject();
//        System.out.printf("The branch %s created!", name);
    }

    public String getName() {
        return name;
    }

    public void writeObject() throws Exception {

        File file = new File(branchPath + File.separator + name);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(this);
    }

    public static Branch deserialize(String filePath) throws IOException, ClassNotFoundException {

        File file = new File(filePath);
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
        Branch branch = (Branch) objectInputStream.readObject();
        return branch;
    }

}
